package cn.logicalthing.demo.web.interceptor;

import cn.logicalthing.demo.web.controller.ControllerBase;
import cn.logicalthing.demo.web.controller.CustomController;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

public class LoginInterceptorScore implements HandlerInterceptor {

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        Map<String,Object> result = getResult(request);
        String code =  result.get(ControllerBase.RESPONSE_CODE).toString();
        if(ControllerBase.SUCCESS_CODE_200.equals(code)){
           String userId =  getUserId(request);
           System.out.println("积分加一");
        }
    }

    public String getUserId(HttpServletRequest request){
       return  request.getAttribute(CustomController.CUSTOM_LOGIN_USER).toString();
    }

    public Map<String,Object> getResult(HttpServletRequest request){
      return   (Map<String,Object>)request.getAttribute(CustomController.CUSTOM_LOGIN_RETURN);
    }
}
